package demo.web.tag;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: zigang.wang
 * Date: 12-3-19
 * Time: 上午11:37
 * To change this template use File | Settings | File Templates.
 */
public class Functions {
    public static boolean contains(Object o1, Object o2) {
        return o1 instanceof Collection && ((Collection) o1).contains(o2);
    }

    public static boolean indexOf(String s1, String s2) {
        if (s1.indexOf(s2) == -1) {
            return false;
        } else {
            return true;
        }
    }

    public static String getDateDir(Calendar cal) {
        return cal.get(Calendar.YEAR) + "_" + cal.get(Calendar.MONTH) + "_" + cal.get(Calendar.DAY_OF_MONTH);
    }

    public static boolean isEmpty(Object o) {
        if (o instanceof Collection) {
            return ((Collection) o).isEmpty();
        } else if (o instanceof Map) {
            return ((Map) o).isEmpty();
        }

        return false;
    }

    public static String abbreviate(String s, int length) {
        return StringUtils.abbreviate(s, length);
    }

    public static String int2String(int fen) {
        return ((fen / 2) + "");
    }


    public static String cnDate(Date date) {
        if (date == null) return null;
        return formatDate(date, "yyyy-MM-dd");
    }

    public static String cnTime(Date date) {
        if (date == null) return "";
        return formatDate(date, "yyyy-MM-dd HH:mm");
    }

    public static String ymTime(Date date) {
        if (date == null) return "";
        return formatDate(date, "yyyy-MM");
    }

    public static String mdTime(Date date) {
        if (date == null) return "";
        return formatDate(date, "MM-dd");
    }

    public static String mdsmTime(Date date) {
        if (date == null) return "";
        return formatDate(date, "MM-dd HH:mm");
    }

    public static String hmTime(Date date) {
        if (date == null) return "";
        return  formatDate(date, "HH:mm");
    }

    public static String formatDate(long time, String pattern) {
        return formatDate(new Date(time), pattern);
    }

    public static String formatDate(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static String cnDate() {
        Date date = new Date();
        return cnDate(date);
    }

    public static String cnDate(String pattern) {
        SimpleDateFormat tempSf = new SimpleDateFormat(pattern);
        return tempSf.format(new Date());
    }

    public static String cnTime() {
        Date date = new Date();
        return cnTime(date);
    }

    public static boolean isToday(String date) {
        SimpleDateFormat sf = new SimpleDateFormat();
        if (StringUtils.isEmpty(date)) {
            return false;
        }
        sf.applyPattern("yyyy-MM-dd");
        try {
            return DateUtils.isSameDay(sf.parse(date), new Date());
        } catch (ParseException e) {
            return false;
        }
    }

    public static String encodeUTF8(String source) {
        try {
            return URLEncoder.encode(source, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

}
